import java.util.LinkedList;
import java.util.NoSuchElementException;

/**
 * @Version :
 * @PROJECT_NAME: java-basics
 * @PACKAGE_NAME:PACKAGE_NAME
 * @NAME: MyQueue
 * @author:xuandian
 * @DATE: 2022/6/25 11:35
 * @description:
 */


public class MyQueue<T> implements Queue<T> {
    private Object[] queue;
    private int size = 0;

    public MyQueue() {
        queue = new Object[6];
    }

    @Override
    public boolean add(T data) throws Exception {
        System.out.println(this.size);
        if (this.size > queue.length) {
            throw new RuntimeException();
        } else {
            queue[this.size++] = data;
        }
        return this.size > queue.length;
    }

    @Override
    public boolean offer(T data) {
        if (this.queue.length != this.size) {
            this.queue[this.size++] = data;
            return true;
        }
        return false;
    }

    @Override
    public T remove() {
        if (this.size == 0) {
            throw new RuntimeException();
        }
        Object data;

        Object[] queue = new Object[this.queue.length - 1];
//        for (int i = 0; i < queue.length; i++) {
//            queue[i] = queue[i + 1];
//        }
        System.arraycopy(queue, 1, queue, 0, queue.length);
        return (T) (data = this.queue[0]);
    }

    @Override
    public T poll() {
        if (this.size == 0) {
            return null;
        }
        Object data;
        Object[] queue = new Object[this.queue.length - 1];
//        for (int i = 0; i < queue.length; i++) {
//            queue[i] = queue[i + 1];
//        }
        System.arraycopy(queue, 1, queue, 0, queue.length);
        return (T) (data = this.queue[0]);
    }

    @Override
    public T element() {
        if (this.size == 0) {
            throw new RuntimeException();
        }
        if (this.size == 0) {
            return null;
        } else {
            return (T) this.queue[0];
        }
    }

    @Override
    public T peek() {
        if (this.size == 0) {
            return null;
        } else {
            return (T) this.queue[0];
        }
    }

    @Override
    public int size() {
        return queue.length;
    }
}
